home *** CD-ROM | disk | FTP | other *** search
- Path: ausnews.austin.ibm.com!usenet
- From: Leo Uzcategui <leou@austin.ibm.com>
- Newsgroups: comp.lang.c++
- Subject: Re: STL: for_each vs transform
- Date: Fri, 01 Mar 1996 14:41:16 -0600
- Organization: IBM Corp.
- Message-ID: <313760EC.41C6@austin.ibm.com>
- References: <3135F631.41C6@austin.ibm.com> <4h7ems$6v@druid.borland.com>
- NNTP-Posting-Host: socks.austin.ibm.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; AIX 1)
-
- Pete Becker wrote:
- >
- > In article <3135F631.41C6@austin.ibm.com>, leou@austin.ibm.com says...
- > >
- > >How can iterate thru a container, using for_each, and apply a function
- > >object which modifies each of the elements in the container?
- > >I tried passing the argument by name, but the template definition
- > >will not allow it.
- > >
- > >Here's the example: Note addr and size values never change.
- > >
- > >#include <iostream.h>
- > >#include <algo.h>
- > >#include <vector.h>
- > >
- > >struct S { vector<int> v; };
- > >struct g : public binary_function<S,int,int> {
- > > int operator()(S s,int x) const
- > > {s.v.push_back(x);
- > > cout<<"x="<<x<<endl;
- > > cout<<"g() addr of s:"<<(size_t)&s<<" size of v:"<<s.v.size()<<endl;
- > > return 0;};
- > >};
- > >
- > >void main() {
- > >vector<S> A(3); // A has 3 vector<int>'s
- > >for (int i=0; i<2; i++)
- > > for_each( A.begin(),A.end(), bind2nd(g(),i) );
- > >}
- >
- > I made three changes in the first few lines of this code. None of them affect
- > the legality of the call to for_each. With those changes, this code compiled
- > just fine with BC++ 5.0. Here are the changed lines:
- >
- > #include <iostream.h>
- > #include <algorothm> // 1: ANSI/ISO header name
- > #include <vector> // 2: ANSI/ISO header name
- >
- > using namespace std; // 3: these things live in namespace std
-
- The sample code compiles and works OK for me too. My question was how
- to pass by name the 1st argument to the function invoked by for-each().
- I thought of using transform(), but its definition doesn't quite fit
- the problem here since the binary function used in transform, requires
- two containers.
-